package com.fau.cm.action.index;

import java.io.BufferedOutputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.cm.action.PublicAction;
import com.fau.cm.entity.MedicalAdmin;
import com.fau.cm.entity.MedicalCallSms;
import com.fau.cm.util.FileNameEncoderUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc  我的短信管理Action
 * @author yangzj
 * @datetime 2014年10月21日,上午11:20:46
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class SmsMyAction extends PublicAction implements ModelDriven<MedicalCallSms> {
	
	private static final long serialVersionUID = 4120353772096266393L;
	
	private MedicalCallSms callSms = new MedicalCallSms();

	/**
	 * @desc 跳转到我的短信管理页面
	 * @return SUCCESS
	 * @author yangzj
	 * @datetime 2014年10月21日,上午11:22:27
	 */
	@Action(value = "sms_my_init", results = {@Result(name = SUCCESS, location = "sms_my_init.jsp")})
	public String init(){
		return SUCCESS;
	}

	/**
	 * @desc 分页查询短信
	 * @return null
	 * @author yangzj
	 * @datetime 2014年10月21日,上午11:51:53
	 */
	@Action(value = "sms_my_view")
	public String view(){
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		this.pageBean = this.baseAllService.getSmsService().findSmsMyByPage(admin, callSms, map);
		this.map.clear();
		this.map.put("total", this.pageBean.getTotalRow());
		this.map.put("data", this.pageBean.getResult());
		formatJSON(response, this.map);
		return null;

	}
	
	/**
	 * @desc 短信信息添加页面
	 * @return SUCCESS
	 * @author yangzj
	 * @datetime 2014年10月21日,上午11:52:36
	 */
	@Action(value = "sms_my_add", results = {@Result(name = SUCCESS, location = "sms_add.jsp")})
	public String add(){
		return SUCCESS;
	}
	
	/**
	 * @desc 查看详细信息页面
	 * @return SUCCESS
	 * @author yangzj
	 * @datetime 2014年10月21日,上午11:54:04
	 */
	@Action(value = "sms_my_view_detail", results = {@Result(name = SUCCESS, location = "sms_view_detail.jsp")})
	public String viewDetail(){
		return SUCCESS;
	}
	
	/**
	 * @desc 导出全部短信信息
	 * @return null
	 * @author yangzj
	 * @datetime 2014年10月21日,上午11:54:18
	 */
	@Action(value = "export_my_all")
	public String exportAll(){
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"短信管理导出模板.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			Workbook workbook = this.baseAllService.getSmsService().exportExcelAllMySms(admin,this.callSms);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 导出当前页短信信息
	 * @return null
	 * @author yangzj
	 * @datetime 2014年10月21日,上午11:54:44
	 */
	@Action(value = "export_my_current")
	public String exportCurrent(){
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"短信管理导出模板.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			Workbook workbook = this.baseAllService.getSmsService().exportExcelCurrentMySms(admin, callSms.getIds());
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@Override
	public MedicalCallSms getModel() {
		return callSms;
	}
}
